#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

bool validTicTacToe(vector<string>& board) {
    int count1 = 0, count2 = 0;
    for (int i = 0; i < board.size(); i++)
        for (int j = 0; j < board[0].size(); j++)
            if (board[i][j] == 'X')  count1++;
            else if (board[i][j] == 'O') count2++;

    return !((count1 != count2 && count2 != count1 - 1) ||
        (count2 != count1 - 1 && win(board, 'X')) ||
        (count2 != count1 && win(board, 'O')));
}

bool win(vector<string>& board, char p)
{
    for (int i = 0; i < 3; i++)
        if ((p == board[0][i] && p == board[1][i] && p == board[2][i]) ||
            (p == board[i][0] && p == board[i][1] && p == board[i][2])) {
            return true;
        }

    return ((p == board[0][0] && p == board[1][1] && p == board[2][2]) ||
        (p == board[0][2] && p == board[1][1] && p == board[2][0]));
}